/*
* @Author: Marte
* @Date:   2017-12-01 15:49:58
* @Last Modified by:   Marte
* @Last Modified time: 2017-12-01 17:56:58
*/

//数字滚动插件
 ;(function($){
  $.fn.numberScroll = function(command,arg){
    var that = this;
    var opts = {
      _w:"24",
      _h:"24",
      _numList:[],
      _number:"",
      _style:["list-style:none",
          "overflow: hidden",
          "height:24px",
          "font-size:23px",
          "position:absolute",
          "margin:0",
          "padding:0",
          "bottom:15px",
          "right:15px",
          "font-family: 'Microsoft YaHei'",
          "display:none",
          "color:#479e98",
      ]};

    var liHtml = '<li style="left:{{l}}px;width:{{w}}px; height:1000%;position:absolute;">{{div}}</li>',
        divHtml = '<div style="width:{{w}}px;height:10%">{{num}}</div>';

    var init = function(n){
      opts._number = n;
      opts._numList = n.toString().split("");
      _build();
    }

    var _build = function(){
      var li="",
          div="";
      var liHtml = '<li style="left:{{l}}px;width:{{w}}px; height:1000%;position:absolute;">{{div}}</li>'
          .replace("{{w}}",opts._w);
      var divHtml = '<div style="width:{{w}}px;height:10%">{{num}}</div>'
          .replace("{{w}}",opts._w).replace("{{h}}",opts._h);
      for(var i=0;i<opts._numList.length;i++){
        for(var j=0;j<10;j++){
          div += divHtml.replace("{{num}}",j)
        }
        li += liHtml.replace("{{l}}",opts._w*i).replace("{{div}}",div);
      }

      that.html("");
      that.append(li);
      that.get(0).style.cssText = opts._style.join(";");
      that.css({width:that.find(">li").length * opts._w + "px"})
      that.fadeIn(1000,function(){
        _move();
      });
    }

    var setNum = function(num){
      var nList = num.toString().split("");
      if(that.find(">li").length != nList.length){
        that.fadeOut(1000,function(){
            opts._numList = nList;
            _build();
            _move();
        })
      }else{
        opts._numList = nList;
        _move();
      }
    }

    var _move = function(speed){
      speed = speed || 1000
      for(var i=0;i<opts._numList.length;i++){
        var n = opts._numList[i];
        that.find(">li").eq(i).animate({"top":-n*opts._h +"px"},speed);
      }
    }
    if(command == "init" || arguments.length ==0){
      num = parseInt(arg) || 0
      init(num);
    }
    if(command == "set"){
      num = parseInt(arg) || 0
      setNum(num);
    }
    if(command == "config"){
      opts = $.extend(opts,arg);
    }
    return this;
  }
})($);

